import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class BasicApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: ParentWidgetC()
    );
  }
}

class Echo extends StatelessWidget{
  const Echo({
    Key key,
    @required this.text,
    this.backgroundColor:Colors.grey,
}):super(key:key);

  final String text;
  final Color backgroundColor;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        color: backgroundColor,
        child: Text(text)
      ),
    );
  }
}

class CounterWidget extends StatefulWidget {
  const CounterWidget({
    Key key,
    this.initValue: 0
  });
  final int initValue;

  @override
  _CounterWidgetState createState()=>new _CounterWidgetState();
}

class _CounterWidgetState extends State<CounterWidget>{
  int _counter;

  @override
  void initState(){
    super.initState();
    _counter = widget.initValue;
    print("initState");
  }
  @override
  Widget build(BuildContext context){
    print("build");
    return Center(
      child: FlatButton(
        child: Text('$_counter'),
        // 点击后计数器自增
        onPressed: ()=>setState(()=>++_counter),
      ),
    );
  }
  @override
  void didUpdateWidget(CounterWidget oldWidget){
    super.didUpdateWidget(oldWidget);
    print("didUpdateWidget");
  }
  @override
  void deactivate(){
    super.deactivate();
    print("deactivate");
  }
  @override
  void dispose(){
    super.dispose();
    print("dispose");
  }
  @override
  void reassemble(){
    super.reassemble();
    print("reassemble");
  }
  @override
  void didChangeDependencies(){
    super.didChangeDependencies();
    print("didChangeDependencies");
  }
}

// TapboxA管理自身状态
class TapboxA extends StatefulWidget {
  TapboxA({Key key}):super(key: key);

  @override
  _TapboxAState createState() => new _TapboxAState();
}

class _TapboxAState extends State<TapboxA>{
  bool _active = false;

  void _handleTap(){
    setState((){
      _active = !_active;
    });
  }

  Widget build(BuildContext context){
    return new GestureDetector(
      onTap: _handleTap,
      child: Container(
        child: Center(
          child: Text(
            _active?'Active':'Inactive',
            style: TextStyle(fontSize: 32.0, color: Colors.white)
          ),
        ),
        width: 200.0,
        height: 200.0,
        decoration: new BoxDecoration(
          color: _active?Colors.lightGreen[700]:Colors.grey[600]
        ),
      ),
    );
  }
}

// ParentWidget 为TapboxB管理状态
class ParentWidget extends StatefulWidget{
  @override
  _ParentWidgetState createState()=> _ParentWidgetState();
}
class _ParentWidgetState extends State<ParentWidget>{
  bool _active = false;
  void _handleTapboxChanged(bool newValue){
    setState(() {
      _active = newValue;
    });
  }
  Widget build(BuildContext context){
    return Container(
      child: TapboxB(
        active: _active,
        onChanged: _handleTapboxChanged,
      )
    );
  }
}
class TapboxB extends StatelessWidget{
  TapboxB({Key key, this.active:false, @required this.onChanged}):super(key:key);
  final bool active;
  final ValueChanged<bool> onChanged;
  void _handleTap(){
    onChanged(!active);
  }
  Widget build(BuildContext context){
    return new GestureDetector(
      onTap: _handleTap,
      child: Container(
        child: Center(
          child: Text(
              active?'Active':'Inactive',
              style: TextStyle(fontSize: 32.0, color: Colors.white)
          ),
        ),
        width: 200.0,
        height: 200.0,
        decoration: new BoxDecoration(
            color: active?Colors.lightGreen[700]:Colors.grey[600]
        ),
      ),
    );
  }
}

// 混合管理
class ParentWidgetC extends StatefulWidget{
  @override
  _ParentWidgetCState createState()=> _ParentWidgetCState();
}
class _ParentWidgetCState extends State<ParentWidgetC>{
  bool _active = false;
  void _handleTapboxChanged(bool newValue){
    setState(() {
      _active = newValue;
    });
  }
  Widget build(BuildContext context){
    return Container(
        child: TapboxC(
          active: _active,
          onChanged: _handleTapboxChanged,
        )
    );
  }
}
class TapboxC extends StatefulWidget{
  TapboxC({Key key, this.active:false, @required this.onChanged}):super(key:key);
  final bool active;
  final ValueChanged<bool> onChanged;
  _TapboxCState createState()=>_TapboxCState();
}
class _TapboxCState extends State<TapboxC>{
  bool _highlight = false;
  void _handleTapDown(TapDownDetails details){
    setState(() {
      _highlight = true;
    });
  }
  void _handleTapUp(TapUpDetails details){
    setState(() {
      _highlight = false;
    });
  }
  void _handleTapCancel() {
    setState(() {
      _highlight = false;
    });
  }
  void _handleTap() {
    widget.onChanged(!widget.active);
  }
  Widget build(BuildContext context){
    // 在按下时添加绿色边框,抬起时,取消高亮
      return new GestureDetector(
        onTapDown: _handleTapDown, // 处理按下
        onTapUp: _handleTapUp, // 处理抬起
        onTap: _handleTap,
        onTapCancel: _handleTapCancel,
        child: Container(
          child: Center(
            child: Text(
                widget.active?'Active':'Inactive',
                style: TextStyle(fontSize: 32.0, color: Colors.white)
            ),
          ),
          width: 200.0,
          height: 200.0,
          decoration: new BoxDecoration(
              color: widget.active?Colors.lightGreen[700]:Colors.grey[600],
              border: _highlight?
              Border.all(
                color: Colors.teal[700],
                width: 10.0)
              :null,
          ),
        ),
      );
    }
}